﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Microsoft.Samples.WindowsPhoneCloud.Phone.Pages
{
    using System;
    using System.Windows;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using Microsoft.Phone.Tasks;
    using Microsoft.Samples.WindowsPhoneCloud.Phone.ViewModel;

    public partial class UploadPhotoPage : PhoneApplicationPage
    {
        public UploadPhotoPage()
        {
            this.InitializeComponent();

            this.ViewModel = new UploadPhotoPageViewModel();
        }

        public UploadPhotoPageViewModel ViewModel
        {
            get { return this.DataContext as UploadPhotoPageViewModel; }
            set { this.DataContext = value; }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var photoResult = GetApplicationState<PhotoResult>("PhotoResult");
            if ((this.ViewModel != null) && (photoResult != null))
            {
                this.ViewModel.LoadPhoto(photoResult);
            }
            else
            {
                this.NavigationService.GoBack();
            }
        }

        private static T GetApplicationState<T>(string key)
        {
            if (!PhoneApplicationService.Current.State.ContainsKey(key))
            {
                return default(T);
            }

            return (T)PhoneApplicationService.Current.State[key];
        }

        private void OnUploadPicture(object sender, RoutedEventArgs e)
        {
            if (this.ViewModel != null)
            {
                if (string.IsNullOrEmpty(this.ViewModel.BlobName))
                {
                    MessageBox.Show("The blob name cannot be null nor empty.", "Upload Photo Result", MessageBoxButton.OK);
                }
                else
                {
                    try
                    {
                        this.ViewModel.UploadPhoto(
                            successMessage =>
                            {
                                this.Dispatcher.BeginInvoke(
                                    () =>
                                    {
                                        MessageBox.Show("Upload successful", "Upload Photo Result", MessageBoxButton.OK);
                                        PhoneApplicationService.Current.State.Remove("PhotoResult");
                                        this.NavigationService.GoBack();
                                    });
                            },
                            errorMessage =>
                            {
                                this.Dispatcher.BeginInvoke(
                                    () =>
                                    {
                                        MessageBox.Show(errorMessage, "Upload Photo Result", MessageBoxButton.OK);
                                    });
                            });
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message, "Upload Photo Result", MessageBoxButton.OK);
                        this.NavigationService.GoBack();
                    }
                }
            }
        }
    }
}